<?php
class GenericFetcher extends AbstractFetcher {
	/**
	 * the html sourcecode
	 * @var string
	 */
	private $source = null;
	
	/**
	 * nothing to set here, because we dont have an ID or something like this
	 * @see AbstractFetcher#setContentInformation()
	 */
	protected function setContentInformation(){
		$this->contentInformation = '';
		return true;
	}
	
	/**
	 * @see AbstractFetcher#setTitle()
	 */
	protected function setTitle(){
		$source = $this->getSource();
		if(preg_match('/<title>\s*(.*)\s*<\/title\>/s', $source, $match) !== false && count($match) == 2){
			$title = $match[1];
			$title = preg_replace('/[\n|\r|\t|\f]/', ' ', $title);
			$title = $string = preg_replace('/\s{2,}/', ' ', $title);
			$this->title = $title;
		}
	}
	
	/**
	 * @see AbstractFetcher#setDescription()
	 */
	protected function setDescription(){
		$source = $this->getSource();
		if(preg_match('/<meta[^>*]name="description"[^>*]content="(.*)".*\/*>/sU', $source, $match) !== false && count($match) == 2){
			$this->description = $match[1];
		}
		elseif(preg_match('/<meta[^>*]content="(.*)"[^>*]name="description".*\/*>/sU', $source, $match) !== false && count($match) == 2){
			$this->description = $match[1];
		}
	}
	
	/**
	 * @see AbstractFetcher#setPreviewImages()
	 */
	protected function setPreviewImages(){
		$source = $this->getSource();
		$images = array();
		preg_match_all('/(img|src)=("|\')[^"\'>]+/i', $source, $media);
		if(count($media) == 0 || $media[0] === array()){
			$ext = strtolower(substr($this->url, -3));
			if($ext == 'jpg' || $ext = 'jpeh' || $ext == 'gif' || $ext == 'png'){
				$this->previewImages[] = $this->url;
			}
			return;
		}
		$data = preg_replace('/(img|src)("|\'|="|=\')(.*)/i', '$3', $media[0]);
		foreach($data as $url){
			if(count($this->previewImages) >= MAX_PREVIEWS_PER_GET){
				break;
			}
			$info = pathinfo($url);
			if(isset($info['extension'])){
				if(($info['extension'] == 'jpg') || ($info['extension'] == 'jpeg') || ($info['extension'] == 'gif') || ($info['extension'] == 'png')){
					if(substr($url, 0, 4) != 'http'){ //if is relative url
						if(substr($url, 0, 1) == '/'){ //if is relative to domain, not to dir
							$lastslash = strpos($this->url, '/', 10);
							$main = substr($this->url, 0, ($lastslash) ? $lastslash : strlen($this->url));
							$url = $main.'/'.$url;
							if(!in_array($url, $this->previewImages)){
								$this->previewImages[] = $url;
							} 
						}
						else{ //if is relative to dir
							if(substr($this->url, -1) == '/'){ //trailing slash -> easy
								$url = $this->url.$url;
							}
							elseif(strpos($this->url, '/', 10) === false){ //no slash at all
								$url = $this->url.'/'.$url;
							}
							else{
								$url = '';
								//TODO: if url is complex (has pathes or files)
							}
							if($url != '' && !in_array($url, $this->previewImages)){
								$this->previewImages[] = $url;
							}
						}
					}
					else{
						if(!in_array($url, $this->previewImages)){
							$this->previewImages[] = $url;
						}
					}
				}
			}
		}
	}
	
	/**
	 * returns html source, it will be fetched just one time
	 * @return string
	 */
	private function getSource(){
		if($this->source === null){
			$this->loadSource();
		}
		return $this->source;
	}
	
	/**
	 * fetches source from url
	 * @return void
	 */
	private function loadSource(){
		require_once('CurlWrapper.php');
		$source = CurlWrapper::getSource($this->url);
		if($source){
			$this->source = $source;
			return true;
		}
		else{
			$this->source = false; //error occurred
			return false;
		}
	}
}